﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System;

namespace System {
    public interface IThemeContext
    {
        string WorkingThemeName { get; }
    }

    public partial class ThemeContext : IThemeContext
    {
        public const string ThemeName = ThemeableViewLocationExpander.THEME_KEY;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IConfiguration _configuration;

        public ThemeContext(IHttpContextAccessor httpContextAccessor, IConfiguration configuration)
        {
            _httpContextAccessor = httpContextAccessor;
            _configuration = configuration;
        }

        public string WorkingThemeName
        {
            get
            {
                string token = _httpContextAccessor?.HttpContext?.Request?.Query[ThemeName];
                if (!String.IsNullOrEmpty(token)) return token;
                token = _httpContextAccessor?.HttpContext?.Request?.Headers[ThemeName];
                if (!String.IsNullOrEmpty(token)) return token;
                //token = _httpContextAccessor?.HttpContext?.Session?.GetString(ThemeName);
                //if (!String.IsNullOrEmpty(token)) return token;
                token = _httpContextAccessor?.HttpContext?.Request?.Cookies[ThemeName];
                if (!String.IsNullOrEmpty(token)) return token;
                token = _configuration?["Appsetting:Themes"];
                return token ?? "Default";
            }
        }
    }
}